package LeetCode.LeetCode2;

public class Num58 {
    public String reverseLeftWords(String s, int n) {
        char[] ch = s.toCharArray();
        if(s == null || n > ch.length - 1){
            return null;
        }
        int left1=0; int right1=n-1;/**n是下标*/
        int left2=n; int right2=ch.length-1; /**翻转字符串的前面n个字符*/
        s = rotateWords(s,left1,right1); /**翻转字符串的后面部分*/
        s = rotateWords(s,left2,right2); /**翻转整个字符串*/
        s = rotateWords(s,left1,right2);
        return s;
    }
    private static String rotateWords(String str,int begin,int end) {
        char[] ch = str.toCharArray();//字符串生成字符数组
        while (begin < end) {
            char tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin++;
            end--;
        }
        return String.copyValueOf(ch);
    }
}
